<?php
	defined( '_JEXEC' ) or die( 'Restricted access' );
	jimport( 'joomla.application.component.model' );
	jimport( 'joomla.methods' );
	include_once(JPATH_COMPONENT.DS.'libraries'.DS.'core.php');
	require_once(JPATH_COMPONENT.DS.'data'.DS.'implFusionTables'.DS.'FTDAOFactory.php');
	require_once(JPATH_COMPONENT.DS.'data'.DS.'implJoomla'.DS.'JDAOFactory.php');
	require_once(JPATH_COMPONENT.DS.'dominio'.DS.'DTOFactory.php');
	
	class ModelSocialRoutesRoute extends JModel
	{
		var $_route = null;
		
		function __construct()
		{
			parent::__construct();
		}
		
		private function addCreateRouteSocialActivity($route){
			$act = new stdClass();
			$act->cmd = 'route.create';
			$act->actor = $route->getUser()->get('id');
			$act->target = 0; // no target
			$act->title = JText::sprintf('CC ROUTE CREATED',JRoute::_('index.php?option=' . JRequest::getCmd('option') .'&task=showRoute&id=' . $route->getId()),$route->getName());
			$act->content = JText::sprintf('CC ROUTE DETAILS',$route->getDistance(),$route->getTime());
			$act->app = 'profile';
			$act->cid = 0;
			
			CFactory::load('libraries', 'activities');
			CActivityStream::add($act);
		}
		
		private function addUploadRouteSocialActivity($route){
			$act = new stdClass();
			$act->cmd = 'route.upload';
			$act->actor = $route->getUser()->get('id');
			$act->target = 0; // no target
			$act->title = JText::sprintf('CC ROUTE UPLOADED',JRoute::_('index.php?option=' . JRequest::getCmd('option') .'&task=showRoute&id=' . $route->getId()),$route->getName());
			$act->content = JText::sprintf('CC ROUTE DETAILS',$route->getDistance(),$route->getTime());
			$act->app = 'profile';
			$act->cid = 0;
			
			CFactory::load('libraries', 'activities');
			CActivityStream::add($act);
		}
		
		private function getIsMyRoute($route){
			$daoUserRoutes=JDAOFactory::getInstance()->getDAOUserRoutes();
			$userRoutes=$daoUserRoutes->getUserRoutes(JFactory::getUser()->get('id'));
			
			$i=0;
			$end=false;
			while(!$end && $i<count($userRoutes)){
				if($userRoutes[$i]->getRouteId() == $route->getId()){
					$end=true;
				}
				$i++;
			}
			return $end;
		}
		
		public function getIsCreator($route){
			if($route->getUser()->get('id')==JFactory::getUser()->get('id')){
				return true;
			}
			return false;
		}
		
		//Sets the route from the DB by using the id in the request
		function readRoute($id){
			$daoJRoute=JDAOFactory::getInstance()->getDAORoute();
			$this->_route=$daoJRoute->getRoute($id);
			return $this->_route;
		}
		
		//Sets and saves the route that comes in the request
		function saveRouteFromRequest(){
			$this->_route=DTOFactory::getInstance()->getDTORouteFromRequest();
			
			$daoRoute=JDAOFactory::getInstance()->getDAORoute();
			$id=$daoRoute->saveRoute($this->_route);
			$this->_route->setId($id);
			
			$daoFTRoute=FTDAOFactory::getInstance()->getDAORoute();
			$daoFTRoute->saveRoute($this->_route);
			
			$this->addCreateRouteSocialActivity($this->_route);
			return $this->_route->getId();
		}
		
		public function saveRouteFromUpload(){
			$this->_route=DTOFactory::getInstance()->getDTORouteFromUpload();
			
			$daoRoute=JDAOFactory::getInstance()->getDAORoute();
			$id=$daoRoute->saveRoute($this->_route);
			$this->_route->setId($id);
			
			$daoFTRoute=FTDAOFactory::getInstance()->getDAORoute();
			$daoFTRoute->saveRoute($this->_route);
			
			$this->addUploadRouteSocialActivity($this->_route);
			return $this->_route->getId();
		}
		
		function addUserRoute(){
			$routeID=JRequest::getVar('id',0);
			$userID =& JFactory::getUser()->get('id');

			$daoUserRoutes=JDAOFactory::getInstance()->getDAOUserRoutes();
			return $daoUserRoutes->addUserRoute($routeID,$userID);
		}
		
		function removeUserRoute(){
			$routeID=JRequest::getVar('id',0);
			$userID =& JFactory::getUser()->get('id');
			
			$daoUserRoutes=JDAOFactory::getInstance()->getDAOUserRoutes();
			return $daoUserRoutes->removeUserRoute($routeID,$userID);
		}
		
		function getRouteComments(){
			$daoComments=JDAOFactory::getInstance()->getDAORouteComment();
			$routeComments=$daoComments->getRouteComments($this->_route->getId());

			return $routeComments;
		}
		
		function addRouteComment(){
			$daoComments=JDAOFactory::getInstance()->getDAORouteComment();
			return $daoComments->saveComment();
		}
		
		function downloadRoute(){
			$parser=new SRParserKML2GPX();
			header("Content-type: text/plain");
  			header("Content-Disposition: attachment; filename=".$this->_route->getName().".gpx");
  			echo $parser->KML2GPX($this->_route->getRawCoordinates());
		}
		
		function getRoute()
		{	
			return $this->_route;
		}
		
		function getMyRoutesTask($route){
			if($this->getIsCreator($route)){
				return DTOFactory::getInstance()->getDTOMyRoutesTaskCreador($route);
			}
			else{
				$isMyRoute=$this->getIsMyRoute($route);
				if($isMyRoute){
					return DTOFactory::getInstance()->getDTOMyRoutesTaskRemove($route);
				}
				else{
					return DTOFactory::getInstance()->getDTOMyRoutesTaskAdd($route);
				}
			}
		}
	}
?>